/*
* ============================================================================
*  Name     : CDummyProjAppUi from DummyProjAppUi.cpp
*  Part of  : DummyProj
*
*  Created by Penrillian, Smartphone Software Innovators 
*  See http://www.penrillian.com
* ============================================================================
*/

// INCLUDE FILES
#include "DummyProjAppUi.h"
#include "DummyProjDialog.h" 
#include <DummyProj.rsg>
#include "dummyproj.hrh"
#include "StackClass.h"

#include <avkon.hrh>
#include <aknquerydialog.h> 
#include <stringloader.h> 

// ================= MEMBER FUNCTIONS =======================
//
// ----------------------------------------------------------
// CDummyProjAppUi::ConstructL()
// 
// ----------------------------------------------------------
//
void CDummyProjAppUi::ConstructL()
    {
    BaseConstructL();

    iAppDialog = new (ELeave) CDummyProjDialog;
	iAppDialog->SetMopParent( this );
    iAppDialog->ExecuteLD( R_DUMMYPROJ_DIALOG );
    AddToStackL( iAppDialog );

	iStack = new (ELeave) CStack;
    }

// ----------------------------------------------------
// CDummyProjAppUi::~CDummyProjAppUi()
// Destructor
// Frees reserved resources
// ----------------------------------------------------
//
CDummyProjAppUi::~CDummyProjAppUi()
    {
    if (iAppDialog)
        {
        RemoveFromStack( iAppDialog );
        delete iAppDialog;
		}
	if(iStack)
	{
			delete iStack;
	}
   }

// ------------------------------------------------------------------------------
// CDummyProjAppUi::DynInitMenuPaneL(TInt aResourceId,CEikMenuPane* aMenuPane)
//  This function is called by the EIKON framework just before it displays
//  a menu pane. Its default implementation is empty, and by overriding it,
//  the application can set the state of menu items dynamically according
//  to the state of application data.
// ------------------------------------------------------------------------------
//
void CDummyProjAppUi::DynInitMenuPaneL(
    TInt /*aResourceId*/,CEikMenuPane* /*aMenuPane*/)
    {
    }

// ----------------------------------------------------
// CDummyProjAppUi::HandleKeyEventL(
//     const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
// takes care of key event handling
// ----------------------------------------------------
//
TKeyResponse CDummyProjAppUi::HandleKeyEventL(
    const TKeyEvent& /*aKeyEvent*/,TEventCode /*aType*/)
    {
    return EKeyWasNotConsumed;
    }

// ----------------------------------------------------
// CDummyProjAppUi::HandleCommandL(TInt aCommand)
// takes care of command handling
// ----------------------------------------------------
//
void CDummyProjAppUi::HandleCommandL(TInt aCommand)
    {
    switch ( aCommand )
        {
        case EEikCmdExit:
            {
            Exit();
            break;
            }
        
		case EDummyProjCmdAdd:
            {
				TInt stackVal=0;
				if(NumberQueryDialogL(stackVal))
				{
					iStack->push(stackVal);
					UpdateListboxL(stackVal, _L("Added"));
				}
				break;
			}

        case EDummyProjCmdRemove:
            {
				TInt poppedVal=iStack->pop();
				UpdateListboxL(poppedVal, _L("Removed"));
				break;
            }

        default:
            break;      
        }
    }


TBool CDummyProjAppUi::NumberQueryDialogL(TInt& aReturnVal)
{
	CAknNumberQueryDialog* stackValueQuery = CAknNumberQueryDialog::NewL(aReturnVal);
	CleanupStack::PushL(stackValueQuery);
	HBufC* prompt=StringLoader::LoadLC(R_STACK_VALUE_QUERY_PROMPT);
	stackValueQuery->SetPromptL(*prompt);
	CleanupStack::PopAndDestroy(prompt);
	CleanupStack::Pop(stackValueQuery);

	return(stackValueQuery->ExecuteLD(R_MY_DATA_QUERY_DIALOG));
}

void CDummyProjAppUi::UpdateListboxL(TInt aStackValue,const TDesC& aText)
{
	TBuf<100> listboxMessage;
	listboxMessage.AppendFormat(_L("\t%S %d"), &aText, aStackValue);
	iAppDialog->AddListboxItemL(listboxMessage);
	listboxMessage.Zero();
	listboxMessage.AppendFormat(_L("\tStacksize=%d "),iStack->CurrentSize());
	iAppDialog->AddListboxItemL(listboxMessage);
}

// End of File  
